﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NetCore.ServiceGroup;
using System;
using WebApiClient;

namespace NetCore
{
    /// <summary>
    /// IServiceCollection 静态扩展 配置微服务或节点类
    /// </summary>
    public static class ServicesConfigureExtensions
    {
        /// <summary>
        /// 将指定节点Json格式数据转为一个新的对象
        /// </summary>
        /// <typeparam name="TOptions"></typeparam>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public static TOptions ConfigureOption<TOptions>(this IServiceCollection services, IConfiguration configuration, Func<TOptions> provider) where TOptions : class
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            if (configuration == null) throw new ArgumentNullException(nameof(configuration));
            if (provider == null) throw new ArgumentNullException(nameof(provider));
            var config = provider();
            configuration.Bind(config);
            services.AddOptions();
            services.Configure<TOptions>(configuration);
            return config;
        }

        /// <summary>
        /// 配置微服务http调用的公共静态方法
        /// </summary>
        /// <param name="services"></param>
        /// <param name="appConfiguration"></param>
        /// <returns></returns>
        public static IServiceCollection RegisterHttpsApi(this IServiceCollection services, IConfiguration appConfiguration)
        {
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            #region 配置表格服务

            Action<HttpApiConfig> configDocumentOptions = c =>
            {
                c.HttpHost = new Uri(appConfiguration["App:MicroServices:Document:ServerRootAddress"]);
                c.GlobalFilters.Add(new DefaultTokenAttribute());
                c.FormatOptions.DateTimeFormat = DateTimeFormats.ISO8601_WithMillisecond;
            };
            //services.AddHttpApi<IFileFolderApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<IFileOtherApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<ITableDataApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<ITableFolderApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<ITemplateFolderApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<ITemplateTableApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<IUploadApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            //services.AddHttpApi<ITemplateExplanationApi>()
            //    .ConfigureHttpApiConfig(configDocumentOptions);
            #endregion

            return services;
        }

        /// <summary>
        /// 添加HttpApi
        /// 返回HttpApi工厂
        /// </summary>
        /// <typeparam name="TInterface">接口类型</typeparam>
        /// <param name="services"></param>
        /// <returns></returns>
        public static HttpApiFactoryBuilder<TInterface> AddHttpApi<TInterface>(this IServiceCollection services)
            where TInterface : class, IHttpApi
        {
            return new HttpApiFactoryBuilder<TInterface>(services);
        }
    }
}
